/*
   Copyright 2012 Sohan Basak

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

/*
 * NOTE: Left and Right Direction are mirrorred
 */

var canvas, context, x, y, speed,i, i2, ver, hor, veloc, score, turnctr


x =  0;
y = 0;
speed = 0;
i=640-25;																// I is the maximum distance travellable inside the current window In X
i2=480-25;
score=0;	
turnctr=0;																//Kept for future
		


/* *
 * Here you have to have a knowledge of Components
 * In Physics any linear motion can be divided into two vector components, the vertical and the horizontal
 * Now, here we divide the motion of the body into two vector components which can have value of 0,1 and -1 indicating the direction
 * Its basically a 2D boolean, if termed loosely..
 * 
 * As the speed is fixed at any specific instance therefore, the two vectors not neccessarily have the  velocity indicator
 * Though we will be forming a true vector by multiplying it.
 * 
 * But in more complicated directional objects like games a clear concept of vectors are needed as they can reduce typing
 * and payload on brain significantly :p
 * 
 * */
ver=0																// The vertical component
hor=0																// The horizintal component


function InitVars(){
	x =  0;
	y = 0;
	speed = 5;
	ver = 0;
	hor = 0;
	score=0;
	turnctr=0;
	UpdateScoreBoard(0,0);
}


function animate() {
					

    reqAnimFrame = window.mozRequestAnimationFrame    ||
                window.webkitRequestAnimationFrame ||
                window.msRequestAnimationFrame     ||
                window.oRequestAnimationFrame
                ;

    reqAnimFrame(animate);




/*Uncomment The Next part to enable Autoanimate but DPAD will not work, dont forget to comment out the next part, animation with game AI.!!
 * Biggest joke..
/*
	if (x>=0 && x<i && y==0){											// If X pos is greater than zero, is less than maximum X distance and if Y is at zero
		TurnTo("left")													// GO along positive X direction
	}
		
	if(x==i && y>=0 && y<i2){										    // If X is at the maximum X distance or EDGE, and Y is greater than or equal to zero, and is less than the maximum Y distance 
		TurnTo("down")													// GO along positive Y direction
	}
		
	if(x<=i && x>0 && y==i2){									      	// If X is less than or equal to maximum X distance, is greater than zero and Y is at the maximum Y distance or EDGE
		TurnTo("right")													// GO along negative X direction
	}
		
	if(x==0 && y>0 && y<=i2){										    // If X is at zero and Y is greater than zero, is less than or equal to the Maximum Y distance
		TurnTo("up")													// GO along the negative Y direction
    }
*/    


// This part is where the whole AI is made

	// Since this function is in a loop, so, adding a little value to the vertical vector will continuously
	// Pull the body downwards, and thats with an accelaration, and thats what gravity exactly is
	// actually it depends on mass but since there is one object..
	// and i have created its weight, if you want it to be heavier or lighter, change this amount,.... 
	ver+=0.02															// It simulates gravity
	
	
	veloc=speed															//Backing up the speed, if it makes a lose, the speed of last moment is still accessible, no work for it,added for future 
	if(x>i || x<0 || y>i2 || y<0){
		speed=0													 		// If it reaches X EDGEs, it wont move in horizontal dir
		alert("You Lose\n\nScore:\t"+score)	
		InitVars();
		}
		
		
		
	if(score>70){
		speed=7
	}
	
	ScoreLevel();



    x+= hor * speed														// The true vector is formed by multiplying the directionbt speed
    y+= ver * speed														// The same case repeated for y axis
    draw();
}


function draw() {
    var canvas  = document.getElementById("canvas");
    var context = canvas.getContext("2d");
    var img= document.getElementById("bg_sprite");

  /*  This part I have commented out, but kept, you can experiment with it
	var grd = context.createLinearGradient(0, 0, 640,480);
	grd.addColorStop(0,"blue");
	grd.addColorStop(0.5, "white")
	grd.addColorStop(1,"green");
	
   
		
	var grd2 = context.createLinearGradient(0, 0, 0,480);
	grd2.addColorStop(0,"blue");
	grd2.addColorStop(0.5, "white")
	grd2.addColorStop(1,"green");
  */
	
	
	var grd=context.createRadialGradient(422,139,70, 420,139,150);
	grd.addColorStop(0,"#E8820A");
	grd.addColorStop(1,"#17268F");

    context.clearRect(0, 0, 640, 480);
    
    context.drawImage(img, 0, 0)
  
    context.fillStyle = grd;
    context.beginPath();
    context.arc(x+25/2 ,y+25/2, 25/2, 0,2*Math.PI);
    context.fill()
    

}





//Animations on the body
function TurnTo(dir){
	
	turnctr++;															// Number of turns, kept for future,if you wanna use, you may do it
	score+=speed;														// Updating Score
	UpdateScoreBoard(score, speed);										// Updating Score Board, The board requires only when we have a propbability to update it
																		// Animating it will just drainage of resource
	
	/* *
	 * The TurnTo direction turns the object by simply changing the vector directions
	 * Therefore the speed variable is left scalar and so, the body can be accelarated or retarded at ANY instance by simply
	 * changing the value of the speed variable,,, therefore the object is at a free motion
	 * */
	 
	// Simple directions
	// If left direction, it will go horizonatlly, but not vertically
	if(dir=="left"){
		ver = 0;
		hor = 1;  
	}
	
	// If right  direction it will go in negative horizontal direction
	if(dir=="right"){
		ver = 0;
		hor = -1;
	}
	
	// If up direction it will go in negative vertical direction
	if(dir=="up"){
		ver = -1;
		hor = 0;
	}
	
	// If down direction it will go vertically but not horizontally
	if(dir=="down"){
		ver = 1;
		hor = 0;
	}
	



	
	// Compound directions
	// If up+left it goes vertically negative and horizontally positive
	if(dir=="up+left"){
		ver = -1;
		hor = 1;
	}

	// If down+left it goes positive in both vertical and horizontal direction
	if(dir=="down+left"){
		ver = 1;
		hor = 1;
	}

	// If up+right it goes negative in both vertical and horizontal direction
	if(dir=="up+right"){
		ver = -1;
		hor = -1;
	}
	
	// If down+right, it goes vertically positive but horizontally negative
	if(dir=="down+right"){
		ver = 1;
		hor = -1
	}
	
}


function CtrlSpeed(level){													// Speed Controller function
	speed+=level
}



function ScoreLevel(){
	if(score < 5){
		speed=1
	}
	
	else if(score >= 5 && score<25){
		speed=2
	}
	
	else if(score >= 25 && score<55){
		speed=3
	}
	
	else if(score >= 55 && score<95){
		speed=4
	}
	
	else if(score >= 95 && score<145){
		speed=5
	}
	
	else if(score >= 145 && score<205){
		speed=6
	}
	
	else if(score >= 205 && score<275){
		speed=7
	}
	
	else if(score >=275){
		speed=8
	}
		
}

animate();
